﻿Imports System.Security
Imports System.Security.Principal
Imports TBCC.Utility

Public Class LoginForm

    ' TODO: 插入代码，以使用提供的用户名和密码执行自定义的身份验证
    ' (请参见 http://go.microsoft.com/fwlink/?LinkId=35339)。 
    ' 随后自定义主体可附加到当前线程的主体，如下所示: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' 其中 CustomPrincipal 是用于执行身份验证的 IPrincipal 实现。 
    ' 随后，My.User 将返回 CustomPrincipal 对象中封装的标识信息
    ' 如用户名、显示名等

    Private _tryTimes As Integer = 0
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If CheckValidate() = False Then
            Exit Sub
        End If

        Try
            If User.Instance.Login(Me.UsernameTextBox.Text.Trim, Me.PasswordTextBox.Text) = False Then
                _tryTimes += 1
                If _tryTimes >= 3 Then
                    Logger.Write(Me.UsernameTextBox.Text.Trim(), "", "登陆失败（密码错误）", tbccLogOperateType.Login)
                    Me.Close()
                End If

                msg.Show("用户名或密码错误，请重新输入")
                Me.PasswordTextBox.Focus()
                Exit Sub
            End If

        Catch ex As Exception
            msg.ShowError("连接数据库失败！")
            Exit Sub

        End Try

        Logger.Write(Me.UsernameTextBox.Text.Trim(), User.Instance.uName, "登陆成功", tbccLogOperateType.Login)

        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Function CheckValidate() As Boolean
        If Me.UsernameTextBox.Text.Trim = "" Then
            Me.UsernameTextBox.Focus()
            Return False
        End If

        If Me.PasswordTextBox.Text.Trim = "" Then
            Me.PasswordTextBox.Focus()
            Return False
        End If

        Return True
    End Function

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

End Class
